import type { FormRules } from 'element-plus'

// 样例表单
export interface ResetPasswordForm {
  newPassword: string
  confirmedPassword: string
}

// 重置密码表单（默认值）
export const defaultResetPasswordForm: ResetPasswordForm = {
  newPassword: '',
  confirmedPassword: '',
}

const confirmPasswordValidator = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== defaultResetPasswordForm.newPassword) {
    callback(new Error('两次输入的密码不一致!'))
  } else {
    callback()
  }
}

// 重置密码表单（验证规则）
export const resetPasswordFormRule: FormRules<ResetPasswordForm> = {
  newPassword: [
    { required: true, message: '新密码不可为空！', trigger: 'blur' },
    { min: 6, max: 20, message: '新密码长度6~20的字符！', trigger: 'blur' },
  ],
  confirmedPassword: [
    { required: true, message: '确认密码不可为空！', trigger: 'blur' },
    { min: 6, max: 20, message: '确认密码长度6~20的字符！', trigger: 'blur' },
    { validator: confirmPasswordValidator, trigger: 'blur' },
  ],
}
